/***************************************************************************

-------------------
    begin                : 
    copyright            : kuzkok
    email                : kuzkok at gmail.com

***************************************************************************
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*   
*   Данная программа является свободным программным обеспечением;         *
*   Вы можете распространять ее и/или модифицировать в соответствии с     *
*   GNU General Public License опубликованной Фондом свободного           *
*   программного обеспечения; используемая версия лицензии - 2 или        *
*   (по вашему выбору) любая более поздняя                                *
***************************************************************************/

#include "objectarchive.h"

ObjectArchive::ObjectArchive(QString theTableName) :
    APDMObject(theTableName)
{
    mHistoricalState = "Неизвестно";
}

ObjectArchive::ObjectArchive(QUuid theEventID, QString theTableName) :
        APDMObject(theEventID, theTableName)
{
    loadValues();
}

ObjectArchive::ObjectArchive(QString theTableName, QString whereClause) :
        APDMObject(theTableName, whereClause)
{
    loadValues();
}

void ObjectArchive::loadValues()
{
    if(mObjectState)
    {
        mCreatedDate = mSelectQuery->value(mSelectQuery->record().indexOf("createddate")).toDateTime();
        mLastModified = mSelectQuery->value(mSelectQuery->record().indexOf("lastmodified")).toDateTime();
        mEffectiveFromDate = mSelectQuery->value(mSelectQuery->record().indexOf("effectivefromdate")).toDate();
        mEffectiveToDate = mSelectQuery->value(mSelectQuery->record().indexOf("effectivetodate")).toDate();
        mCreatedBy = mSelectQuery->value(mSelectQuery->record().indexOf("createdby")).toString();
        mModifiedBy = mSelectQuery->value(mSelectQuery->record().indexOf("modifiedby")).toString();
        mHistoricalState = mSelectQuery->value(mSelectQuery->record().indexOf("historicalstate")).toString();
        mProcessFlag = mSelectQuery->value(mSelectQuery->record().indexOf("processflag")).toString();
        mRemarks = mSelectQuery->value(mSelectQuery->record().indexOf("remarks")).toString();
        mOriginEventID = QUuid(mSelectQuery->value(mSelectQuery->record().indexOf("origineventid")).toString());
    }
}

void ObjectArchive::buildInsertStatement()
{
    APDMObject::buildInsertStatement();
    mInsertStatementFields += ", effectivefromdate"
                             ", effectivetodate"
                             ", historicalstate"
                             ", processflag"
                             ", remarks"
                             ", origineventid";
    mInsertStatementValues += ", :effectivefromdate"
                             ", :effectivetodate"
                             ", :historicalstate"
                             ", :processflag"
                             ", :remarks"
                             ", :origineventid";
}

void ObjectArchive::buildUpdateStatement()
{
    mUpdateStatement = "effectivefromdate = :effectivefromdate"
                      ", effectivetodate = :effectivetodate"
                      ", historicalstate = :historicalstate"
                      ", processflag = :processflag"
                      ", remarks = :remarks"
                      ", origineventid = :origineventid";

}

void ObjectArchive::bindQueryValues(QSqlQuery &query)
{
    APDMObject::bindQueryValues(query);
    query.bindValue(":effectivefromdate", mEffectiveFromDate);
    query.bindValue(":effectivetodate", mEffectiveToDate);
    query.bindValue(":historicalstate", mHistoricalState);
    query.bindValue(":processflag", mProcessFlag);
    query.bindValue(":remarks", mRemarks);
    query.bindValue(":origineventid", UuidToVariant(mOriginEventID));

}
